#此模块用于确定后续维修段的状态，删除状态等操作在choose_action中实现

import pandas as pd
# import modin.pandas as pd
flight_state = pd.read_pickle(r'C:\Users\HP\Desktop\adaRLAMRP\case_pkl\case8.8.pkl')#读取航班状态信息
aircraft_action =pd.read_pickle(r'C:\Users\HP\Desktop\adaRLAMRP\case_pkl\飞机8.8.pkl')#读取航班状态信息#读取飞机行为可选信息
base_inf = pd.read_pickle(r'C:\Users\HP\Desktop\adaRLAMRP\case_pkl\基地8.pkl')#读取航班状态信息#读取基地信息，用于基地容量约束

# flight_state = pd.read_csv(r'C:\Users\HP\Desktop\adaRLAMRP\case\case8.8.csv',encoding='gbk')#读取航班状态信息
# aircraft_action = pd.read_csv(r'C:\Users\HP\Desktop\adaRLAMRP\case\飞机8.8.csv')#读取飞机行为可选信息
# base_inf = pd.read_csv(r'C:\Users\HP\Desktop\adaRLAMRP\case\基地8.csv',encoding='gbk')#读取基地信息，用于基地容

def visit_main(flight_time,final_time,final_air,base_cap,base,all_cap,flight_num,states):
    flight_time = flight_time
    final_time = final_time
    final_air = final_air
    base_cap = base_cap
    base = base
    all_cap = all_cap
    flight_num = flight_num
    states = states
    TMAX = 2400  # 两次维修间最大允许累计飞行时间
    base1 = [x for x in base if base_cap[base.index(x)] <= all_cap[base.index(x)]]
    available_flight= flight_state[[x in states for x in flight_state.index]]
    available_flight = available_flight[[x == final_air for x in available_flight.dep_air]]
    available_flight = available_flight[[x in base1 for x in available_flight.arr_air]]
    # available_flight = available_flight[[x - final_time >=45 for x in available_flight.dep_time]]
    # print(available_flight)
    # available_flight = available_flight[[x + flight_time <= TMAX for x in available_flight.flight_time]]
    # available_flight = flight_state[flight_state.index.isin(states)]  # 筛选出可用状态中的剩余航班 用于查找后续航班
    # available_flight = available_flight[available_flight['dep_air'] == final_air]  # 机场限制
    # available_flight = available_flight[(available_flight['dep_time'] - final_time) > 45]  # 衔接时间限制
    # available_flight = available_flight[(available_flight['flight_time'] + flight_time) <= TMAX]  # 累计飞行时间限制
    # available_flight = available_flight[available_flight['arr_air'].isin(base1)]  # 筛选到达机场属于base的多个元素，保障到达机场是维修机场
    if not available_flight.empty:
        available_flight = available_flight[(available_flight['dep_time'] - final_time) > 45]  # 衔接时间限制
        available_flight = available_flight[(available_flight['flight_time'] + flight_time) <= TMAX]  # 累计飞行时间限制
        if not   available_flight.empty:
         available_flight.loc[:, '衔接时间'] = available_flight.loc[:, 'dep_time'] - final_time
         maintain_num = available_flight['衔接时间'].idxmin()  # 数值最小所在的索引，即代表状态
         punish = False
         return maintain_num, punish
        else:
            maintain_num = flight_num  # 就地维修时需要保证到达机场是不是维修机场，以考虑是否存在惩罚值
            if flight_state.at[maintain_num, 'arr_air'] in base1:
                punish = False
            else:
                punish = True
            return maintain_num, punish
    else:
        maintain_num = flight_num #就地维修时需要保证到达机场是不是维修机场，以考虑是否存在惩罚值
        if flight_state.at[maintain_num,'arr_air'] in base1:
            punish = False
        else:
            punish = True
        return  maintain_num,punish
















